/*
 * Copyright (C) 2014 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.camera;

import rpg.RPG;
import rpg.gfx.Bitmap;
import rpg.gfx.TextAlign;

/**
 *
 * @author Rothens
 */
public class CEText extends CinematicEffect {

    private int length = 0;
    private int currentTick = 0;
    Bitmap text = null;
    private int x = 10;
    private int y = 510;
    private int fadeTime = 0;
    private static final int MAX_FADE_TIME = 60;
    private int fadeVal = 0;
    private int currentAlpha = 0;
    
    public CEText(String text, int length){
        this.length = length;
        this.text = RPG.fs.getMultilineText(text, (byte)2, RPG.WINDOW_WIDTH-x*2, 0xffff00, 0, TextAlign.CENTER);
        this.y = 550-this.text.height/2;
        fadeTime = Math.min(MAX_FADE_TIME, length/2);
        fadeVal = 255/fadeTime;
    }
    
    public CEText(String text, int length, int x, int y){
        this(text, length);
        this.x = x;
        this.y = y;
    }
    

    @Override
    public boolean tick() {
        currentTick++;
        if(currentTick < fadeTime){
            currentAlpha += fadeVal;
            text.setAlpha((byte)currentAlpha);
        }
        if(currentTick == fadeTime){
            text.setAlpha((byte)0xff);
            currentAlpha = 0xff;
        }
        if(currentTick > length-fadeTime){
            currentAlpha -= fadeVal;
            text.setAlpha((byte)currentAlpha);
        }
        
        return currentTick >= length;
    }

    @Override
    public Bitmap getBitmap(Bitmap toDrawOn) {
        toDrawOn.drawOnBlend(text, x, y);
        return toDrawOn;
    }
}
